package com.neusoft.neuiotms.dcam.common.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.bean.TreeBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.common.dto.LocationDTO;
import com.neusoft.neuiotms.dcam.common.service.LocationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "位置管理")
@RestController
@RequestMapping(value = "/api/w/locations")
public class LocationController {

    @Autowired
    private LocationService locationService;

    @ApiOperation("查询所有位置")
    @GetMapping
    public ResultListDTO<LocationDTO> index() {
        final List<LocationDTO> DTOs = this.locationService.findAll();
        final ResultListDTO<LocationDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有位置")
    @GetMapping("/p")
    public PageResultDTO<LocationDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.locationService.search(pageable, searchable));
    }

    @ApiOperation("创建位置")
    @PostMapping
    public ResultDTO<LocationDTO> create(@RequestBody final LocationDTO dto) {
        final LocationDTO result = this.locationService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("位置详情")
    @GetMapping("/{id}")
    public ResultDTO<LocationDTO> details(@PathVariable final Long id) {
        final LocationDTO dto = this.locationService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除位置")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.locationService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改位置")
    @PutMapping("/{id}")
    public ResultDTO<LocationDTO> update(@PathVariable final Long id,
            @RequestBody final LocationDTO dto) {
        final LocationDTO result = this.locationService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("获取位置树")
    @GetMapping("/tree")
    public ResultListDTO<TreeBean> getLocationTree() {
        final List<TreeBean> tree = this.locationService.locationTree();
        return ResultListDTO.success(tree);
    }
}
